package com.vegetable.modules.controller.outbound;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.outbound.po.DeductionAddPO;
import com.vegetable.modules.entity.outbound.po.DeductionQueryPO;
import com.vegetable.modules.entity.outbound.po.SalesOutSequencePO;
import com.vegetable.modules.entity.outbound.vo.DeductionVO;
import com.vegetable.modules.service.outbound.ISalesOutDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 销售总表 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-04-01
 */
@Slf4j
@RestController
@RequestMapping("/outStorage/salesOut")
@AllArgsConstructor
@Api(value = "销售总表", tags = "销售总表")
public class SalesOutDetailController extends AbstractController {

    private ISalesOutDetailService detailService;
    /**
     * 所有销售总表列表
     */
    @PostMapping("/sequence")
    @ApiOperation(value = "条件查询销售总表列表", notes = "条件查询销售总表列表")
    public R sequence(@RequestBody SalesOutSequencePO po){
        log.info("条件查询销售总表列表{}", JSON.toJSONString(po));
        PageUtils page = detailService.sequenceToDetail(po);
        return R.data( page);
    }

    @PostMapping("/deductionList")
    @ApiOperation(value = "条件查询扣款列表", notes = "条件查询扣款列表")
    public R deductionList(@RequestBody DeductionQueryPO po) throws Exception {
        log.info("条件查询扣款列表{}", JSON.toJSONString(po));
        if(NumberUtil.equals(BigDecimal.ZERO, po.getDeductionMoney()) && ObjectUtil.isNull(po.getCustomerId())){
            throw new Exception("新增扣款时 请选择客户");
        }
        IPage<DeductionVO> page = detailService.deductionList(po);
        return R.data( page);
    }

    @PostMapping("/deductionAdd")
    @ApiOperation(value = "条件查询扣款列表", notes = "条件查询扣款列表")
    public R deductionAdd(@RequestBody List<DeductionAddPO> po){
        log.info("条件查询扣款列表{}", JSON.toJSONString(po));
        detailService.deductionAdd(po);
        return R.data("");
    }

    @GetMapping("/deductionDel")
    @ApiOperation(value = "删除扣款", notes = "删除扣款")
    public R deductionDel(Long id){
        log.info("删除扣款：{}", id);
        detailService.deductionDel(id);
        return R.data("");
    }

}
